package com.elevator;

import java.awt.Dimension;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;

import data.setting;
import java.util.Timer;
import java.util.TimerTask;

public class simulationUI extends JPanel {
	private static final long serialVersionUID = 1L;
	public Vector<elevatorViews> mElevatorViews = new Vector<elevatorViews>();
    public JButton mBackBtn = new JButton("Back");
	private int mPersonCount = 0;
	private int [] mFromLayer=null;
	private int [] mPersonInLift = null;
	private int [] mToLayer = null;
	private int [] mArrLayer = null;
	private personUI []mPersonUIs = null;
        private boolean mbResetLift = false;
        private Timer mClearPersonTimer = null;
	public simulationUI(int personCount, int []fromLayer,int []toLayer)
	{
		mPersonCount = personCount ;
		mFromLayer = fromLayer;
		mToLayer = toLayer;
		this.setLayout(null);
		if (fromLayer != null) {
			mPersonInLift = new int[fromLayer.length];
			mArrLayer = new int [fromLayer.length];
			for (int i = 0; i < mPersonInLift.length; i++) {
				mPersonInLift[i] = -1;
				mArrLayer[i] = -1;
			}
		}
		if (mPersonCount >=0) {
			mPersonUIs= new personUI[mPersonCount];
			for (int i = 0; i < mPersonUIs.length; i++) {
				mPersonUIs[i] = new personUI();
				
				if(mFromLayer[i] >=0)
				{
					this.add(mPersonUIs[i]);
					mPersonUIs[i].setText(i+1, mToLayer[i]);
					mPersonUIs[i].setLocation(150 +i*60,100+((setting.sLayerCount -1 )-mFromLayer[i]) *100);
				}
			}
		}

		createpanel();
		
		if (mPersonCount >0) {
			for (int i = 0; i < mPersonUIs.length; i++) {
				if(mFromLayer[i] >=0)
				{
					int ndv = mToLayer[i] - mFromLayer[i];
					if (ndv > 0) {
						mElevatorViews.get(0).mLayerUIs[mFromLayer[i]].mUpBtn.doClick();
						}
					else if (ndv < 0) {
						mElevatorViews.get(0).mLayerUIs[mFromLayer[i]].mDownBtn.doClick();
					}
				}
			}
		}
		
	}
	public simulationUI()
	{
		this(0, null, null);
	}
	public void createpanel()
	{
		for (int i = 0; i < mElevatorViews.size(); i++) {
			this.remove(mElevatorViews.get(i));
		}
		mElevatorViews.clear();
		for (int i = 0; i < 1; i++) {
			elevatorViews view1 = new elevatorViews(false);
			view1.mDoorUIEventListener =new doorUIEventListener() {
				
				@Override
				public void doorUIEvent(doorUIStatus nStatus) {
					if (nStatus == doorUIStatus.doorUIOpened) {
						int nIndex = mElevatorViews.get(0).mLiftUI.getCurLayer();
						
						//cur layer
						for (int j = 0; j < mPersonCount; j++) {
							if (mFromLayer[j] == nIndex && mArrLayer[j] != 1) {
								int nNowStatus = mElevatorViews.get(0).mElevatorCtrl.mElevator.mMovingStatus;
								int nWillStatus = mToLayer[j] - mFromLayer[j];
								if (nNowStatus != 1 && nWillStatus <=0) {
									mArrLayer[j] = 0;
									mElevatorViews.get(0).setPersonNum(mElevatorViews.get(0).getPersonNum()+1);
									mPersonUIs[j].setVisible(false);
									mElevatorViews.get(0).mLiftUI.mBtns[mToLayer[j]].doClick();
								}
								else if (nNowStatus != -1 && nWillStatus >=0) {
									mArrLayer[j] = 0;
									mElevatorViews.get(0).setPersonNum(mElevatorViews.get(0).getPersonNum()+1);
									mPersonUIs[j].setVisible(false);
									mElevatorViews.get(0).mLiftUI.mBtns[mToLayer[j]].doClick();
								}

							}
							
							if (mToLayer[j] == nIndex&& mArrLayer[j] == 0) {

								mArrLayer[j] = 1;
								mElevatorViews.get(0).setPersonNum(mElevatorViews.get(0).getPersonNum()-1);
								
								
								mPersonUIs[j].setLocation(150 +j*60,100+((setting.sLayerCount -1 )-mToLayer[j]) *100);
								mPersonUIs[j].setVisible(true);
							}
							

							
						}
						
					}
                                        else if(nStatus ==doorUIStatus.doorUIReset )
                                        {
                                           
                                            if(!simulationUI.this.mbResetLift && testArrayArrivate())
                                            {
                                                  simulationUI.this.mbResetLift = true;
                                                   simulationUI.this.startClearPerson();
                                            }
                                        }
					
				}
			};
			this.add(view1);
			view1.setLocation(300*i, 50);
			mElevatorViews.add(view1);
		}

	    this.add(mBackBtn);
	    mBackBtn.setBounds(120, 10, 80, 30);
		this.setPreferredSize(new Dimension(300, mElevatorViews.get(0).getSize().height+50));
	}
	public void stopTimer() {
		// TODO Auto-generated method stub
		for (int i = 0; i < mElevatorViews.size(); i++) {
			mElevatorViews.get(i).StopTimer();
		}
	}
        private Boolean testArrayArrivate()
        {
            boolean noAllArrivate = true;
            if (mPersonCount > 0)
            {
                for (int i = 0; i < mPersonUIs.length; i++)
                {
                    if (mFromLayer[i] >= 0 && mToLayer[i] >= 0 && mArrLayer[i] != 1)
                    {
                        noAllArrivate = false;
                    }
                }
            }
              return noAllArrivate;
        }
        
        public void startClearPerson()
        {
            mClearPersonTimer = new Timer();
            mClearPersonTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				 mClearPersonTimer.cancel();
                                if (mPersonCount > 0)
                                {
                                    for (int i = 0; i < mPersonUIs.length; i++)
                                    {
                                       mPersonUIs[i].setVisible(false);
                                    }
                                }
			}

		} , 2000, 1000);
        }

}
